<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改日期" prop="updateDate">
        <el-date-picker clearable
          v-model="queryParams.updateDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="登陆token" prop="accessToken">
        <el-input
          v-model="queryParams.accessToken"
          placeholder="请输入登陆token"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="语言" prop="language">
        <el-input
          v-model="queryParams.language"
          placeholder="请输入语言"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题" prop="colorSchema">
        <el-input
          v-model="queryParams.colorSchema"
          placeholder="请输入主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时区" prop="timezone">
        <el-input
          v-model="queryParams.timezone"
          placeholder="请输入时区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登陆时间" prop="lastLoginTime">
        <el-date-picker clearable
          v-model="queryParams.lastLoginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后登陆时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="is_authenticated" prop="isAuthenticated">
        <el-input
          v-model="queryParams.isAuthenticated"
          placeholder="请输入is_authenticated"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="is_active" prop="isActive">
        <el-input
          v-model="queryParams.isActive"
          placeholder="请输入is_active"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="is_anonymous" prop="isAnonymous">
        <el-input
          v-model="queryParams.isAnonymous"
          placeholder="请输入is_anonymous"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登陆方式" prop="loginChannel">
        <el-input
          v-model="queryParams.loginChannel"
          placeholder="请输入登陆方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限" prop="isSuperuser">
        <el-input
          v-model="queryParams.isSuperuser"
          placeholder="请输入权限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:ragflowUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:ragflowUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:ragflowUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:ragflowUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ragflowUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改日期" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登陆token" align="center" prop="accessToken" />
      <el-table-column label="用户名" align="center" prop="nickname" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="avatar" align="center" prop="avatar" />
      <el-table-column label="语言" align="center" prop="language" />
      <el-table-column label="主题" align="center" prop="colorSchema" />
      <el-table-column label="时区" align="center" prop="timezone" />
      <el-table-column label="最后登陆时间" align="center" prop="lastLoginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="is_authenticated" align="center" prop="isAuthenticated" />
      <el-table-column label="is_active" align="center" prop="isActive" />
      <el-table-column label="is_anonymous" align="center" prop="isAnonymous" />
      <el-table-column label="登陆方式" align="center" prop="loginChannel" />
      <el-table-column label="用户状态" align="center" prop="status" />
      <el-table-column label="权限" align="center" prop="isSuperuser" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:ragflowUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:ragflowUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改ragflow用户列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改日期" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="登陆token" prop="accessToken">
          <el-input v-model="form.accessToken" placeholder="请输入登陆token" />
        </el-form-item>
        <el-form-item label="用户名" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="avatar" prop="avatar">
          <el-input v-model="form.avatar" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="语言" prop="language">
          <el-input v-model="form.language" placeholder="请输入语言" />
        </el-form-item>
        <el-form-item label="主题" prop="colorSchema">
          <el-input v-model="form.colorSchema" placeholder="请输入主题" />
        </el-form-item>
        <el-form-item label="时区" prop="timezone">
          <el-input v-model="form.timezone" placeholder="请输入时区" />
        </el-form-item>
        <el-form-item label="最后登陆时间" prop="lastLoginTime">
          <el-date-picker clearable
            v-model="form.lastLoginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后登陆时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="is_authenticated" prop="isAuthenticated">
          <el-input v-model="form.isAuthenticated" placeholder="请输入is_authenticated" />
        </el-form-item>
        <el-form-item label="is_active" prop="isActive">
          <el-input v-model="form.isActive" placeholder="请输入is_active" />
        </el-form-item>
        <el-form-item label="is_anonymous" prop="isAnonymous">
          <el-input v-model="form.isAnonymous" placeholder="请输入is_anonymous" />
        </el-form-item>
        <el-form-item label="登陆方式" prop="loginChannel">
          <el-input v-model="form.loginChannel" placeholder="请输入登陆方式" />
        </el-form-item>
        <el-form-item label="权限" prop="isSuperuser">
          <el-input v-model="form.isSuperuser" placeholder="请输入权限" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRagflowUser, getRagflowUser, delRagflowUser, addRagflowUser, updateRagflowUser } from "@/api/business/ragflowUser";

export default {
  name: "RagflowUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ragflow用户列表表格数据
      ragflowUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createDate: null,
        updateDate: null,
        accessToken: null,
        nickname: null,
        password: null,
        email: null,
        avatar: null,
        language: null,
        colorSchema: null,
        timezone: null,
        lastLoginTime: null,
        isAuthenticated: null,
        isActive: null,
        isAnonymous: null,
        loginChannel: null,
        status: null,
        isSuperuser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickname: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        isAuthenticated: [
          { required: true, message: "is_authenticated不能为空", trigger: "blur" }
        ],
        isActive: [
          { required: true, message: "is_active不能为空", trigger: "blur" }
        ],
        isAnonymous: [
          { required: true, message: "is_anonymous不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询ragflow用户列表列表 */
    getList() {
      this.loading = true;
      listRagflowUser(this.queryParams).then(response => {
        this.ragflowUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        createDate: null,
        updateTime: null,
        updateDate: null,
        accessToken: null,
        nickname: null,
        password: null,
        email: null,
        avatar: null,
        language: null,
        colorSchema: null,
        timezone: null,
        lastLoginTime: null,
        isAuthenticated: null,
        isActive: null,
        isAnonymous: null,
        loginChannel: null,
        status: null,
        isSuperuser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加ragflow用户列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRagflowUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改ragflow用户列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRagflowUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRagflowUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ragflow用户列表编号为"' + ids + '"的数据项？').then(function() {
        return delRagflowUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/ragflowUser/export', {
        ...this.queryParams
      }, `ragflowUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
